package com.leyou.item.controller;

import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import com.leyou.item.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * http://api.leyou.com/api/item/brand/page?key=&page=1&rows=5&sortBy=id&desc=false
 * @author linzx
 * @date 2020/3/25
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key",required = false ) String key,
            @RequestParam(value = "page",defaultValue = "1" ) Integer page,
            @RequestParam(value = "rows",defaultValue = "5" ) Integer rows,
            @RequestParam(value = "sortBy",required = false ) String sortBy,
            @RequestParam(value = "desc",required = false ) Boolean desc
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(key,page,rows,sortBy,desc));
    }

    @DeleteMapping("bid/{bid}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("bid") Long bid) {
        int i = brandService.deleteByPrimaryKey(bid);
        return i == 1 ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();
    }

    @PostMapping()
    public ResponseEntity<Void> InsertBrand(Brand brand ,@RequestParam("cids") List<Long> cids) {
       brandService.InserBrandAndCategoryId(brand,cids);
              return ResponseEntity.ok().build();
    }

    @PutMapping()
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        brandService.updateBrand(brand,cids);
        return  ResponseEntity.ok().build();
    }
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> findBrandByCid(@PathVariable("cid") Long cid) {
        return ResponseEntity.ok(brandService.findBrandByCid(cid));
    }

    /**
     * 根据id查询品牌
     * @param cid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<Brand> findBrandById(@PathVariable("bid") Long cid) {
        return ResponseEntity.ok(brandService.selectByPrimaryKey(cid));
    }

    /**
     * 根据ids查询品牌
     * @param ids
     * @return
     */
    @GetMapping("bid/list")
    public ResponseEntity<List<Brand>> findBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(brandService.selectByIdList(ids));
    }


}
